﻿#include"SList.h"

void SLTPrint(SLTNode* phead)
{
	SLTNode* pcur = phead;
	while (pcur != NULL)
	{
		printf("%d -> ", pcur->data);
		pcur = pcur->next;
	} 
	printf("NULL\n");
}

SLTNode* SLTBuyNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	//链表为空
	SLTNode* newnode = SLTBuyNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLTNode* pcur = *pphead;
		while (pcur->next != NULL)
		{
			pcur = pcur->next;
		}
		pcur->next = newnode;
	}
}

//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = SLTBuyNode(x);

	newnode->next = *pphead;
	*pphead = newnode;
}

//尾删
void SLTPopBack(SLTNode** pphead)
{
	//链表为空不能删除
	assert(pphead && *pphead);
	//链表只有一个结点
	if ((*pphead)->next != NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* pcur = NULL;
		SLTNode* ptail = *pphead;
		while (ptail->next !=NULL )
		{
			pcur = ptail;
			ptail = ptail->next;
		}
		pcur->next = NULL;
		free(ptail);
		ptail = NULL;
	}

}

//头删
void SLTPopFront(SLTNode** pphead)
{
	//链表不能为空
	assert(pphead && *pphead);
	//链表只有一个结点
	SLTNode* pcur = *pphead;
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		pcur = pcur->next;
		free(*pphead);
		*pphead = pcur;
	}

}

//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* pcur = phead;
	while (pcur)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}

//在指定位置之前插⼊数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead && pos);
	SLTNode* newnode = SLTBuyNode(x);
	//pos指向头结点的时候需要特殊处理
	if (pos == *pphead)
	{
		//头插
		SLTPushFront(pphead, x);
	}
	else
	{
		//找pos前一个结点
		SLTNode* pcur = *pphead;
		while (pcur->next != pos)
		{
			pcur = pcur->next;
		}
		pcur->next = newnode;
		newnode->next = pos;
	}
}

//在指定位置之后插⼊数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	SLTNode* newnode = SLTBuyNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

//删除pos结点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pos);
	assert(pphead && *pphead);
	if (*pphead == pos)
	{
		//头删
		SLTPopFront(pphead);
	}
	else
	{
		SLTNode* pcur = NULL;
		SLTNode* ptail = *pphead;
		while (ptail->next != pos)
		{
			ptail = ptail->next;
		}
		pcur = pos->next;
		ptail->next = pcur;
		free(pos);
	}
}

// 删除pos的前一个结点
void SLTEraseBefore(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead && *pphead);
	assert(pos);

	// 如果pos是头结点，前面没有结点可删
	if (*pphead == pos)
	{
		return;
	}

	// 如果头结点是pos的前一个结点
	if ((*pphead)->next == pos)
	{
		SLTPopFront(pphead);
		return;
	}

	SLTNode* prevPrev = *pphead;
	while (prevPrev->next->next != pos) {
		prevPrev = prevPrev->next;
	}

	SLTNode* toDelete = prevPrev->next;
	prevPrev->next = pos;
	free(toDelete);
}

//删除pos之后的结点
void SLTEraseAfter(SLTNode* pos)
{
	assert(pos && pos->next);
	SLTNode* del = pos->next;
	pos->next = del->next;
	free(del);
	del = NULL;
}

//销毁链表
void SListDestroy(SLTNode** pphead)
{
	assert(pphead);
	SLTNode* pcur = *pphead;
	while (pcur)
	{
		SLTNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	*pphead = NULL;
}

//反转链表
SLTNode* reverseList(SLTNode* head)
{
	//链表为空
	SLTNode* pcur = head;//用于遍历原链表
	SLTNode* newhead = NULL;//新链表的头指针
	SLTNode* next = NULL;  // 临时保存下一个节点

	if (head == NULL)
	{
		return;
	}
	else
	{
		while (pcur != NULL)
		{
			next = pcur->next;  // 保存当前节点的下一个节点
			pcur->next = newhead;  // 将当前节点插入到新链表的头部
			newhead = pcur;  // 更新新链表的头指针
			pcur = next;  // 移动到下一个节点
		}
	}
	return newhead;  // 返回反转后链表的头指针
}